%Tests for the egdst system

% get ready (repeated from start.m)
clear 
clear classes
addpath(['.' filesep],['..' filesep]);
clc


tests={
'cake eating problem with discount==1.0'
'cake eating problem with discount==0.75'
'deterministic deaton model interest=discount a0==0'
'deterministic deaton model interest=discount a0<<0'
'deaton model interest=discount a0<<0 and income shocks'
'deterministic retirement problem with discontinuous consumption'
'retirement problem with discontinuous consumption and income shocks'
};

%choice of test
fprintf('Available tests\n');
for d=1:numel(tests)
    fprintf('[%d] %s\n',d,tests{d});
end
fprintf('[%d] %s\n',numel(tests)+1,'all of the above');
ch=input('Which one to run? ','s');
ch=str2num(ch(1));
if isempty(ch) || ~ismember(ch,[1:numel(tests)+1])
    fprintf('Irrelevant input\n');
    return
end
chs=1:numel(tests);
if ch<=numel(tests)
    chs=ch;
end
for ch=chs %CYCLE OVER ALL TESTS
    
fprintf('\nRunning ''%s''\n',tests{ch});


%prepare common
test=egdstmodel(['test' sprintf('%0d',ch)],'tmp_test');
%time and space
test.t0=1;
test.T=30;
%grids
test.mmax=10;           %max cash-at-hand                    
test.ngridm=100;        %standard number of grid points in M
test.ngridmax=1000;     %max number of grid point in M
test.nthrhmax=100;     %max number of treshold points in TH
test.ny=10;            %number of points in discrete representation of income shocks


%run chosen test
switch ch
case {1,2,3,4,5}
% basic model (Deaton)
%states and decisions
test.s={'Dummy state',{0,'dummy'}};
test.trpr={'true',[1]};
test.feasible={'defaultfeasible',true};
test.d={'Dummy decision',{0,'dummty'}};
test.choiceset={'defaultallow',true};
%utility
%test.u={'utility','(pow(consumption,1-crra)-1)/(1-crra)'};
%test.param={'crra','CRRA coefficient in utility',1.2};
%test.u={'marginal','pow(consumption,-crra)'};
%test.u={'marginalinverse','pow(mutility,-1/crra)'};
test.u={'utility','log(consumption)'};
test.u={'marginal','1/consumption'};
test.u={'marginalinverse','1/mutility'};
test.u={'extrap','log(x)'};
%budget
if ch==1
    test.budget={'cashinhand','savings'};
    test.budget={'marginal','1'};
    test.discount='1';
elseif ch==2
    test.budget={'cashinhand','savings'};
    test.budget={'marginal','1'};
    test.discount='.75';
else
    test.budget={'cashinhand','savings*(1+interest)+shock'};
    test.budget={'marginal','1+interest'};
    test.discount='1/(1+interest)';
    test.param={'interest','return on savings',0.01};
end
if ch<=3
    test.a0=0;
elseif ch==5
    test.a0=-6; %fails with lower a0 - CHECK
else 
    test.a0=-test.mmax;
end
%shock
test.shock='lognormal';
if ch==5
    test.shock={'sigma','1'};
else
    test.shock={'sigma','0'};
end
%test.shock={'mu','-0.5*sigma*sigma'};
test.shock={'mu','-0.0111'};
simplot='mack';


case {6,7}
% retirement models (with 2 discrete choices)
%states and decisions
test.s={'Dummy state',{0,'dummy'}};
test.trpr={'true',[1]};
test.feasible={'defaultfeasible',true};
test.d={'Labour supply',{0,'retire',1,'work'}};
test.choiceset={'defaultallow',true};
%utility
test.u={'utility','log(consumption)+duw*(dc1==0)'};
test.param={'duw','disutility of work',1.3}; %Theorem1 problem
%test.param={'duw','disutility of work',1};
test.u={'marginal','1/consumption'};
test.u={'marginalinverse','1/mutility'};
test.u={'extrap','log(x)'};
%budget
test.eq={'wage','Wage','1','next'};
test.budget={'cashinhand','savings+wage*shock*(dc1==1)'};
test.budget={'marginal','1'};
%shock
test.shock='lognormal';
if ch==6
    test.shock={'sigma','0'}; %in this case shock=expectation=1
elseif ch==7
    test.shock={'sigma','.1'};
end
test.shock={'mu','-0.5*sigma*sigma'};
test.discount='.95';
test.a0=0;

simplot='mack-d';
test.cflags.STABILIZE=1;

case 8
case 9
otherwise
end

test.cflags.VERBOSE=0;

%solve, plot and sim

delete out.txt %diary
diary out.txt %diary

test.compile
test.solve

diary off %diary

test.plot1 (tests{ch},'c');
test.plot1 (tests{ch},'vf')

%sims
test.sim([1 3.0])
test.plot2(tests{ch},simplot)

%save with numeric name 
eval(['test' num2str(ch) '=test;']);


end %OF CYCLE OVER ALL TESTS

